package com.feihong.txgw.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.json.user.ProductOrderDetailEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.adapter
 * @class describe:
 * @author wangziheng
 * @time 2017/8/16 13:40
 * @change
 * @chang time
 * @class describe
 */
public class ProductOrderDetailGoodsAdapter extends BaseAdapter {
    private Context mContext;
    private List<ProductOrderDetailEntity> mOrderGoods;
    public ProductOrderDetailGoodsAdapter(Context context){
        mContext = context;
        mOrderGoods = new ArrayList<>();
    }

    public void setGoods(List<ProductOrderDetailEntity> goods){
        mOrderGoods = goods;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mOrderGoods.size();
    }

    @Override
    public Object getItem(int i) {
        return mOrderGoods.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.product_order_detail_goods_item_layout,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.initView(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.initData(position);
        return convertView;
    }

    class ViewHolder{
        ImageView mGoodImg;
        TextView mGoodsName;
        TextView mGoodsDesc;
        TextView mGoodsPrice;
        TextView mGoodsNum;

        public void initView(View convertView){
            mGoodImg = (ImageView) convertView.findViewById(R.id.product_order_detail_orderGoods_item_img);
            mGoodsDesc = (TextView) convertView.findViewById(R.id.product_order_detail_orderGoods_item_desc);
            mGoodsName = (TextView) convertView.findViewById(R.id.product_order_detail_orderGoods_item_phone_deploy);
            mGoodsPrice = (TextView) convertView.findViewById(R.id.product_order_detail_orderGoods_item_price);
            mGoodsNum = (TextView) convertView.findViewById(R.id.product_order_detail_orderGoods_item_num);
        }

        public void initData(int position){

            StringBuffer stringBuffer = new StringBuffer();
            Glide.with(mContext).load(mOrderGoods.get(position).getSku_thumb()).into(mGoodImg);
            try {
                JSONArray jsonArray = new JSONArray(mOrderGoods.get(position).getSku_spec());
                for(int i = 0 ; i < jsonArray.length() ; i ++){
                    JSONObject jsonObject = jsonArray.optJSONObject(i);
                    if(i == 2){
                         stringBuffer.append(jsonObject.optString("name")+":"+jsonObject.optString("value")+"G");
                     }else {
                            stringBuffer.append(jsonObject.optString("name")+":"+jsonObject.optString("value")+"  ");
                     }
                }
                mGoodsDesc.setText(stringBuffer.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            mGoodsPrice.setText("¥ "+String.valueOf(mOrderGoods.get(position).getSku_price()));
            mGoodsNum.setText("x"+mOrderGoods.get(position).getBuy_nums());
            mGoodsName.setText(mOrderGoods.get(position).getSku_name());
        }

    }
}
